package com.wjy.icu.emqx.utils;

import com.wjy.icu.common.exception.BusinessException;
import com.wjy.icu.domain.vo.UpData;
import com.wjy.icu.emqx.client.MqttPushClient;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;

/**
 * @Description:
 * @author: wangjunyong
 * @date: 2021/10/19 10:00
 */
@Slf4j
public class MqttUtils {


    //订阅消息硬件数据
    public static void subscribe() throws MqttException {
        MqttPushClient.getClient().subscribe("");
    }
    public static String Bytes2HexString(byte[] b) {
        String ret= "";
        for (int i = 0; i < b.length; i++) {
            String hex= Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex= '0' +hex;
            }
            ret+=hex.toUpperCase();

        }
        return ret;
    }
    public static UpData upPayloadParse(String upPayload){
        // 68 02 01 01 20 1D0A A207 DB08 0000 D101 9302 1E01 2D01 1E00 2300 1F00 0F00 0F00 0F00 0F00 0F00 A2D7 16
        // 68 02 01 01 20 1D0A 6E07 CC08 0000 F001 A502 1E01 2D01 1E00 2300 1F00 0F00 0F00 0F00 0F00 0F00 08D4 16
        // 68 02 01 01 24 4B0A A608 0209 0000 A401 D600 1301 FE01 2B00 1900 0F00 0F00 0F00 1F00 1F00 0F00 0000 0000 64D9 16
        // 68 02 01 01 24 0D0A BA08 2409 0000 A801 D600 0301 FE01 1E00 1900 0F00 0F00 0F00 1F00 1F00 0F00 0000 0000 A2B5 16
//        log.info("数据："+upPayload);
        //1.校验数据
        if(upPayload.length()<80){
            log.error("数据不正确");
            return null;
        }
        String head                         = upPayload.substring(0,4);                  //头
        String direct                       = upPayload.substring(4, 6);                 //方向
        String serviceNum                   = upPayload.substring(6, 8);                 //服务号
        String length                       = upPayload.substring(8, 10);                //长度
        String mainTemp                     = switch4Str(upPayload.substring(10, 14));   //主温度
        String buttomTemp                   = switch4Str(upPayload.substring(14, 18));   //底部温度
        String eTemp                        = switch4Str(upPayload.substring(18, 22));   //环境温度
        String petTemp                      = switch4Str(upPayload.substring(22, 26));   //宠物温度
        String humidity                     = switch4Str(upPayload.substring(26, 30));   //湿度
        String oxygenConcentration          = switch4Str(upPayload.substring(30, 34));   //氧气浓度
        String mainTempSetting              = switch4Str(upPayload.substring(34, 38));   //温度设定值
        String buttomTempSetting            = switch4Str(upPayload.substring(38, 42));   //底部温度设定值
        String humiditySetting              = switch4Str(upPayload.substring(42, 46));   //湿度设定值
        String oxygenConcentrationSetting   = switch4Str(upPayload.substring(46, 50));   //氧气设定值
        String cycleState                   = switch4Str(upPayload.substring(50, 54));   //循环状态
        String negativeIonSwitch            = switch4Str(upPayload.substring(54, 58));   //负离子开关
        String uv                           = switch4Str(upPayload.substring(58, 62));   //UV灯
        String floodlight                   = switch4Str(upPayload.substring(62, 66));   //照明灯
        String inspectionLamp               = switch4Str(upPayload.substring(66, 70));   //检查灯
        String petSite                      = switch4Str(upPayload.substring(70, 74));   //宠物测量部位
        String atomizerTreatmentTime        = switch4Str(upPayload.substring(74, 78));   //雾化时间
        String infraredPhysiotherapyTime    = switch4Str(upPayload.substring(78, 82));   //红外时间
        //2.方向判断
        if (!direct.equalsIgnoreCase("01")){
            log.error("数据方向不正确");
            return null;
        }
        //3.填装数据
        UpData upData = new UpData(head,
                                    direct,
                                    serviceNum,
                                    length,
                Double.parseDouble(Integer.parseInt(mainTemp, 16)+"")/100+"",
                Double.parseDouble(Integer.parseInt(buttomTemp, 16)+"")/100+"",
                Double.parseDouble(Integer.parseInt(eTemp, 16)+"")/100+"",
                Double.parseDouble(Integer.parseInt(petTemp, 16)+"")/10+"",
                Double.parseDouble(Integer.parseInt(humidity, 16)+"")/10+"",
                Double.parseDouble(Integer.parseInt(oxygenConcentration, 16)+"")/10+"",
                Double.parseDouble(Integer.parseInt(mainTempSetting, 16)+"")/10+"",
                Double.parseDouble(Integer.parseInt(buttomTempSetting, 16)+"")/10+"",
                Double.parseDouble(Integer.parseInt(humiditySetting, 16)+"")+"",
                Double.parseDouble(Integer.parseInt(oxygenConcentrationSetting, 16)+"")+"",
                Integer.parseInt(cycleState, 16)+"",
                Integer.parseInt(negativeIonSwitch, 16)+"",
                Integer.parseInt(uv, 16)+"",
                Integer.parseInt(floodlight, 16)+"",
                Integer.parseInt(inspectionLamp, 16)+"",
                Integer.parseInt(petSite, 16)+"",
                Integer.parseInt(atomizerTreatmentTime, 16)+"",
                Integer.parseInt(infraredPhysiotherapyTime, 16)+"",
                upPayload);
//        log.info("updata:"+upData);
        return upData;
    }
    public static String switch4Str(String temp){
        if (temp.length()!=4){
            throw new BusinessException("转换错误");
        }
        StringBuffer sb = new StringBuffer();
       return sb.append(temp.substring(2,4)).append(temp.substring(0,2)).toString();
    }
    //4字节 10进制转16进制高位在前地位在后
    public static String lowHigh(String var0){
        Float v = Float.parseFloat(var0)*10;
        int i = v.intValue();
        int var1 = 1;
        int var2 = i >> 8;
        int var3 = i & 255;
        String var4 = Integer.toHexString(var2);
        String var5 = Integer.toHexString(var3);
        if(var4.length() > 2) {
            do {
                if(var1 > 1) {
                    var2 >>= 8;
                }
                var4 = Integer.toHexString(var2 >> 8);
                var5 = var5 + Integer.toHexString(var2 & 255);
                ++var1;
            } while(var4.length() > 2);
        }
        if(var4.length() < 2) {
            var4 = "0" + var4;
        }
        if(var5.length() < 2) {
            var5 = "0" + var5;
        }
        return var5 + var4;
    }
    //2字节  10进制转16进制高位在前地位在后
    public static String lowHigh2(String var0){
        Float v = Float.parseFloat(var0);
        int i = v.intValue();
        int var1 = 1;
        int var2 = i >> 8;
        int var3 = i & 255;
        String var4 = Integer.toHexString(var2);
        String var5 = Integer.toHexString(var3);
        if(var4.length() > 2) {
            do {
                if(var1 > 1) {
                    var2 >>= 8;
                }
                var4 = Integer.toHexString(var2 >> 8);
                var5 = var5 + Integer.toHexString(var2 & 255);
                ++var1;
            } while(var4.length() > 2);
        }
        if(var4.length() < 2) {
            var4 = "0" + var4;
        }
        if(var5.length() < 2) {
            var5 = "0" + var5;
        }
        return var5;
    }
    public static void main(String[] args) {
        int i = 31;
        int var1 = 1;
        int var2 = i >> 8;
        int var3 = i & 255;
        String var4 = Integer.toHexString(var2);
        String var5 = Integer.toHexString(var3);
        if(var4.length() > 2) {
            do {
                if(var1 > 1) {
                    var2 >>= 8;
                }
                var4 = Integer.toHexString(var2 >> 8);
                var5 = var5 + Integer.toHexString(var2 & 255);
                ++var1;
            } while(var4.length() > 2);
        }
        if(var4.length() < 2) {
            var4 = "0" + var4;
        }
        if(var5.length() < 2) {
            var5 = "0" + var5;
        }
        System.out.println(var5);
    }
}
